﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.ServiceModel.Channels;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Test : System.Web.UI.Page
{
    public static int currentPageNumber = 1;
    public static int currentNumber = 1;

    //刷新或者跳页
    public static DataTable PageSkipRefresh(int pageNumber)
    {
        int recordNumberStart = (pageNumber - 1) * 10+1;
        //每一页为10条记录的情况下
        int recordNumberEnd = recordNumberStart + 9;
        string sqlcommand = String.Format("select top({0}) * from Test where BirthCertificateNumber not in(select top({1}) BirthCertificateNumber from Test)", recordNumberEnd, recordNumberStart);
        DataTable s = new DataTable();
        s=GetResult(sqlcommand);
        currentPageNumber = pageNumber;
        return s;
        
    }
    //sql执行者
    public static DataTable GetResult(string sql)
    {
        string constr = "Data Source=.;Initial Catalog=HongYangTest;Integrated Security=True";
        DataTable dataTable = new DataTable();
        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, constr);
        sqlDataAdapter.Fill(dataTable);
        return dataTable;

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //绑定数据
        DataTable s = GetResult("select * from Test");
        DataShow.DataSource = s;
        DataShow.DataBind();
        //timer事件一直调用js
        Timer timer = new Timer();
        timer.Tick += delegate
        {
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "EnterToNext", "<script>enterkey();</script>");
        };
        timer.Interval = 20;
        timer.Enabled = true;//不用打开？
                             //显示当前页数和宗页数
        int count = int.Parse((GetResult("select count(*) from Test").Rows[0][0]).ToString());
        if (count % 10 == 0)
        { count=count/ 10 + 1; xx.Text = currentPageNumber + "/" + count; }
        else
        { count =count/10+1; xx.Text = currentPageNumber + "/" + count; }

        if (currentPageNumber < 1)
        {
            ++currentPageNumber;
        }


    }
    //如果分娩地点为其他 分娩框开启
    protected void fenmiandidian_SelectedIndexChanged(object sender, EventArgs e)
    {
        //autopostback属性要为true
        if (fenmiandidian.SelectedValue == "其他")
        {
            fenmian.Enabled = true;
        }
        else
        {
            fenmian.Enabled = false;
        }
    }
    //加载当前医院的对应接生员
    protected void jieshengyiyuan_SelectedIndexChanged(object sender, EventArgs e)
    {
        jieshengyuan.Items.Clear();
        if (jieshengyiyuan.SelectedValue =="福建省妇幼保健院")
        {
            jieshengyuan.Items.Add(new ListItem("李春燕","李春燕"));
            jieshengyuan.Items.Add(new ListItem("王晓明", "王晓明"));
        }
        if (jieshengyiyuan.SelectedValue == "福建省立医院")
        {
            jieshengyuan.Items.Add(new ListItem("吴喜", "张霞"));
        }
    }

    protected void Cancel_Click(object sender, EventArgs e)
    {
        //Response.Write("取消点击已执行");
        chushengzhenghao.Text = "";
        Name.Text = "";
        zhuyuanhao.Text = "";
        zhuyuanriqi.Text = "";
        chuyuanriqi.Text = "";
        chushengriqi.Text = "";
        chushengshijian.Text = "";
        fenmian.Text = "";
        taici.Text = "";
        chushengtizhong.Text = "";
        chushengshenchang.Text = "";
        chushengyunzhou_tian.Text = "";
        chushengyunzhou_zhou.Text = "";
        //TextBox1.Text = "";
       // Response.Write("<Script Language=JavaScript>alert('消息！');</Script>");
    }
    //如果使用模糊查询 日期输入框不能用
    protected void QueryMode_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (QueryMode.SelectedValue == "出生证号模糊" || QueryMode.SelectedValue == "住院号模糊" || QueryMode.SelectedValue == "姓名模糊")
        {
            Start_dengjiriqi.Enabled = false;
            End_dengjiriqi.Enabled = false;
        }
        else
        {
            Start_dengjiriqi.Enabled = true;
            End_dengjiriqi.Enabled = true;
        }
    }
    //记录提交
    protected void Submit_Click(object sender, EventArgs e)
    {
        string dateOfRegistration = DateTime.Now.ToString("yyyy-MM-dd");
        string birthCertificateNumber = chushengzhenghao.Text;
        string name = Name.Text;
        string sex = Sex.SelectedValue;
        string hospitalizationNumber = zhuyuanhao.Text;
        string birthday = chushengriqi.Text;
        string midwifeHospital = jieshengyiyuan.SelectedValue;
        //住院日期大于出院日期和出生日期（分娩日期）
        if (zhuyuanriqi.Text.CompareTo(chuyuanriqi.Text) == -1 && zhuyuanriqi.Text.CompareTo(chushengriqi.Text) == -1)
        {
            //提交到数据库
            string sqlcommmand = String.Format("insert into Test Values({0},'{1}','{2}',{3},'{4}','{5}','{6}')", birthCertificateNumber, name, sex, hospitalizationNumber, birthday, midwifeHospital, dateOfRegistration);
            GetResult(sqlcommmand);
            //然后刷新
            DataShow.DataSource = PageSkipRefresh(currentPageNumber);
            DataShow.DataBind();
            //刷新页数不刷新影响不大 但频繁刷新对服务器压力大
            /* int count = int.Parse((GetResult("select count(*) from Test").Rows[0][0]).ToString());
             if (count % 10 == 0)
             { count = count / 10 + 1; xx.Text = currentPageNumber + "/" + count; }
             else
             { count = count / 10 + 1; xx.Text = currentPageNumber + "/" + count; }*/
            //清空输入框
            chushengzhenghao.Text = "";
            Name.Text = "";
            zhuyuanhao.Text = "";
            zhuyuanriqi.Text = "";
            chuyuanriqi.Text = "";
            chushengriqi.Text = "";
            chushengshijian.Text = "";
            fenmian.Text = "";
            taici.Text = "";
            chushengtizhong.Text = "";
            chushengshenchang.Text = "";
            //Response.Write("保存成功");
            Response.Write("<Script Language=JavaScript>alert('保持成功！');</Script>");
    }
        else
        {
            Console.WriteLine("没了要用js才能提示 所以要用input 然后用的全是Textbox");
            Response.Write("住院日期应大于出院日期和出生日期");
            //Response.Write("<Script Language=JavaScript>alert('消息！');</Script>");
        }
    }
     //记录查询
    protected void Select_Click(object sender, EventArgs e)
    {
        string date = DateTime.Now.ToString("yyyy-MM-dd");
        string queryMode = QueryMode.SelectedValue;
        string queryData = QueryData.Text;
        string start_dengjiriqi= Start_dengjiriqi.Text;
        string end_dengjiriqi= End_dengjiriqi.Text;
        string sqlommand = "select * from Test where";
        //默认当天
        //Response.Write(date+queryMode+queryData+start_dengjiriqi+ end_dengjiriqi+ sqlommand);
        if (start_dengjiriqi == "")
        {
            sqlommand = sqlommand + String.Format(" DateOfRegistration ='{0}'", date);
        }
        //正常查询
        else
        {
            if (queryMode.Equals("出生证号"))
            {
                sqlommand = sqlommand + String.Format(" BirthCertificateNumber ={0} and DateOfRegistration>'{1}' and DateOfRegistration<'{2}'", queryData, start_dengjiriqi, end_dengjiriqi);
            }
            if (queryMode.Equals("住院号"))
            {
                sqlommand = sqlommand + String.Format(" HospitalizationNumber ={0} and DateOfRegistration>'{1}' and DateOfRegistration<'{2}'", queryData, start_dengjiriqi, end_dengjiriqi);
            }
            if (queryMode.Equals("姓名"))
            {
                sqlommand = sqlommand + String.Format(" Name ='{0}' and DateOfRegistration>'{1}' and DateOfRegistration<'{2}'", queryData, start_dengjiriqi, end_dengjiriqi);
            }
            if (queryMode.Equals("出生证号模糊"))
            {
                sqlommand = sqlommand + String.Format(" BirthCertificateNumber like '%{0}%' and DateOfRegistration>'{1}' and DateOfRegistration<'{2}'", queryData, start_dengjiriqi,end_dengjiriqi);
            }
            if (queryMode.Equals("住院号模糊"))
            {
                sqlommand = sqlommand + String.Format(" HospitalizationNumber like '%{0}%' and DateOfRegistration>'{1}' and DateOfRegistration<'{2}'", queryData, start_dengjiriqi, end_dengjiriqi);
            }
            if (queryMode.Equals("姓名模糊"))
            {
                sqlommand = sqlommand + String.Format(" Name  like '%{0}%'' and DateOfRegistration>'{1}' and DateOfRegistration<'{2}'", queryData, start_dengjiriqi, end_dengjiriqi);
            }

        }
        Response.Write(sqlommand);
        DataTable s = GetResult(sqlommand);
        DataShow.DataSource = s;
        DataShow.DataBind();
    }
    //点击编辑触发事件
        protected void DataShow_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //进入编辑
        DataShow.EditIndex = e.NewEditIndex;
        //重新绑定 数据
        DataShow.DataBind();
        //Response.Write("点击编辑执行到这");
        //DataShow.DataSource=DataShow.DataSource;
    }
    //点击删除出发事件
    protected void DataShow_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //Response.Write("<script>return confirm('are you sure?')</script>");

        //数据绑定 
        //int index = DataShow.SelectedIndex;//选哪里都等于-1？
        //    Response.Write(index);
        //string s1 = DataShow.Rows[index].Cells[0].Text;//第一列数据

        //Button btn = sender as Button;
        //GridViewRow row = btn.Parent.Parent as GridViewRow;
        //string a = row.Cells[0].ToString();//获得第一个单元格的值  
        //Response.Write(a);
        //string b =DataShow.DataKeys[row.DataItemIndex].Values[0];//获得DataKeys的值 
        //获取到主键
        string primaryKey = DataShow.DataKeys[e.RowIndex].Value.ToString();
       // Response.Write(primaryKey);
        string sqlcommand = String.Format("delete from Test where BirthCertificateNumber={0}",primaryKey);
        GetResult(sqlcommand);
        DataShow.DataSource = PageSkipRefresh(currentPageNumber);
        DataShow.DataBind();
        //事件需要表单支持

    }
    //点击更新触发事件
    protected void DataShow_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //int index = DataShow.SelectedIndex;//选哪里都等于-1？
        //string s1 = DataShow.Rows[index].Cells[0].Text;
        //string s2 = DataShow.Rows[index].Cells[1].Text;
        //string s3 = DataShow.Rows[index].Cells[2].Text;
        //string s4 = DataShow.Rows[index].Cells[3].Text;
        //string s5 = DataShow.Rows[index].Cells[4].Text;
        //string s6 = DataShow.Rows[index].Cells[5].Text;
        //string s7 = DataShow.Rows[index].Cells[6].Text;
        //string sqlcommand = String.Format("update Test set BirthCertificateNumber={0},Name='{1}',Sex='{2}',HospitalizationNumber={3},Birthday='{4}',MidwifeHospital='{5}',DateOfRegistration='{6}'",s1,s2,s3,s4,s5,s6,s7);
        //GetResult(sqlcommand);
        ////刷新
        //DataShow.DataSource = PageSkipRefresh(currentPageNumber);
        //DataShow.DataBind();
        string primaryKey = DataShow.DataKeys[e.RowIndex].Value.ToString();
        string sqlcommand = String.Format("select * from Test where BirthCertificateNumber={0}",primaryKey);
        DataTable s = GetResult(sqlcommand);
        chushengzhenghao.Text=s.Rows[0][0].ToString();
        Name.Text= s.Rows[0][1].ToString();
        //Sex.Text=s.Rows[0][2].ToString();
        if ("女".Equals(s.Rows[0][2].ToString()))
        {
            Sex.Items.FindByValue("男").Selected = false;
            Sex.Items.FindByValue("女").Selected = true;
        }
        zhuyuanhao.Text= s.Rows[0][3].ToString();
        chushengriqi.Text= s.Rows[0][4].ToString();
        //jieshengyiyuan.Text = s.Rows[0][5].ToString();
        if ("福建省立医院".Equals(s.Rows[0][5].ToString()))
        {
            jieshengyiyuan.Items.FindByValue("福建省妇幼保健院").Selected = false;
            jieshengyiyuan.Items.FindByValue("福建省立医院").Selected = true;
        }

    }
    //点击取消触发事件
    protected void DataShow_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {

        //DataShow.DataSource = PageSkipRefresh(currentPageNumber);
        //DataShow.DataBind();

       DataShow.EditIndex = -1;
       DataShow.DataBind();

    }
    //上一页
    protected void Page_Back_Click(object sender, EventArgs e)
    {
        if (currentPageNumber > 1)
        {
            --currentPageNumber;
            DataShow.DataSource = PageSkipRefresh(currentPageNumber);
            DataShow.DataBind();
        }
    }
    //下一页
    protected void Page_Next_Click(object sender, EventArgs e)
    {
        int count = int.Parse((GetResult("select count(*) from Test").Rows[0][0]).ToString());
        if (currentPageNumber <count)
        {
            ++currentPageNumber;
            DataShow.DataSource = PageSkipRefresh(currentPageNumber);
            DataShow.DataBind();
        }
    }
    //修改每页条数
    protected void PageSelected_SelectedIndexChanged(object sender, EventArgs e)
    {
       string s= PageSelected.SelectedValue;
        DataShow.PageSize = int.Parse(s);
        //刷新
        DataShow.DataSource = PageSkipRefresh(currentPageNumber);
        DataShow.DataBind();
    }

    protected void Cancel_Command(object sender, CommandEventArgs e)
    {

    }

}